package com.demo.controller;

import com.demo.entity.user.User;
import com.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {

    private Logger log= LoggerFactory.getLogger(this.getClass());
    private UserService userService = null;

    @Autowired
    public UserController(UserService userService) {
        log.info("-------injected class userService = "+userService);
        this.userService = userService;
    }


    @RequestMapping("/login")
    public String login(HttpServletRequest request,Model model){
        String userName=request.getParameter("username");
        String password=request.getParameter("password");
        if (userName == null||password == null) {
            model.addAttribute("msg",null);
            System.out.println("----返回頁面 username = " + userName +" ,password = "+password);
            return "login";//返回login页面
        }
        User user= userService.findByUserAndPass(userName,password);
        System.out.println("----从数据查出的数据 user = " + user);
        System.out.print(user!=null?"----用户id:"+user.getId():"用户不存在");
        if (user == null) {
            model.addAttribute("msg","用户不存在 ");
        }else{
            model.addAttribute("msg","你好:"+user.getUsername());
        }
        return "login";//跳转到WEB-INF/views/login.jsp页面
        //return user!=null?"index":"error";
    }

}
